<?php

namespace App\Services;

use App\Models\CashApply;
use App\Models\User;

/**
 * 提现服务
 * Class CashApplyService
 * @package App\Services
 */
class CashApplyService extends BaseService
{
    /**
     * 生成唯一订单号
     * @return string
     */
    public static function createOrderNo()
    {
        list($msec, $sec) = explode(' ', microtime());
        $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        $orderNo = 'ca' . $msectime . mt_rand(10000, 99999);

        if (CashApply::whereOrderNo($orderNo)->exists()) {
            $orderNo = 'ca' . $msectime . mt_rand(10000, 99999);
        }
        return $orderNo;
    }

    /**
     * 余额提现打款成功
     * @param $cashApply
     */
    public static function balanceCheckSuccess($cashApply)
    {
        if (!$cashApply) {
            return self::ierror(-1, '申请记录不存在');
        }

        $balance = $cashApply->balance;

        $remark = "到账金额:" . $balance;
        if ($cashApply->commission > 0) {
            $remark .= ',手续费:' . $cashApply->commission;
        }

        $user = User::find($cashApply->uid);
        if (!$user) {
            return self::ierror(-1, '用户不存在');
        }

        OfficialTemplateService::sendWithdrawSuccess($cashApply);
    }

    /**
     * 余额提现驳回
     * @param CashApply $cashApply
     * @param $failMsg string 驳回原因
     * @throws \Throwable
     */
    public static function balanceCheckFail($cashApply, $failMsg = '')
    {
        $cashApply->fail_time = time();
        $cashApply->fail_msg = $failMsg;
        $cashApply->save();

        $data = [
            'thing2' => [
                'value' => '余额提现',
            ],
            'phrase3' => [
                'value' => '已驳回',
            ],
            'date8' => [
                'value' => date('Y-m-d H:i:s'),
            ],
            'thing5' => [
                'value' => '驳回原因:' . $failMsg
            ]
        ];
        OfficialTemplateService::sendWithdrawFail($cashApply);
        $user = User::find($cashApply->uid);
        if (!$user) {
            exception('用户不存在');
        }

        $balance = $cashApply->balance;

        $remark = "审核驳回退回余额:" . $balance;

        $reduceBalance = $cashApply->balance + $cashApply->commission;
        $user->balance = max(0, $user->balance + $reduceBalance);

        $ret = $user->saveOrFail();

        if (!$ret) {
            exception('用户余额更新失败');
        }

        //插入余额明细
        BalanceService::addCreditLog(23, $cashApply->uid, 1, $reduceBalance, $user->balance, $remark, 0, 0, 0, $user->give_balance);

        return $cashApply;
    }

    /**
     * 余额提现申请成功
     * @param $cashApply
     * @throws \Throwable
     */
    public static function extractBalanceSuccess($cashApply)
    {
        $user = User::find($cashApply->uid);
        if (!$user) {
            exception('用户不存在');
        }

        $balance = $cashApply->balance;

        $remark = "到账金额:" . $balance;
        if ($cashApply->commission > 0) {
            $remark .= ',手续费:' . $cashApply->commission;
        }

        $reduceBalance = $cashApply->balance ;
        $user->balance = max(0, $user->balance - $reduceBalance);

        $ret = $user->saveOrFail();

        if (!$ret) {
            exception('用户余额更新失败');
        }

        //插入余额明细
        BalanceService::addCreditLog(22, $cashApply->uid, 1, -$reduceBalance, $user->balance, $remark, 0, 0, 0, $user->give_balance);

        return $cashApply;
    }
}
